package com.maltys.greedy;

/**
 * @author malty
 * @link <a href="https://leetcode.cn/problems/monotone-increasing-digits/">单调递增的数字</a>
 */
public class MonotoneIncreasingDigits {
    public int monotoneIncreasingDigits(int n) {
        String value = String.valueOf(n);
        char[] chars = value.toCharArray();
        int flag = chars.length;
        for (int i = chars.length - 1; i > 0; i--) {
            if (chars[i - 1] > chars[i]) {
                chars[i - 1] -= 1;
                flag = i;
            }
        }
        for (int i = flag; i < chars.length; i++) {
            chars[i] = '9';
        }
        return Integer.parseInt(new String(chars));
    }

    public static void main(String[] args) {
        MonotoneIncreasingDigits digits = new MonotoneIncreasingDigits();
        int increasingDigits = digits.monotoneIncreasingDigits(1234);
        System.out.println(increasingDigits);
    }
}
